package com.ld.admin.controller.usercenter.countdown;

import java.util.Date;

import com.ld.admin.model.countdown.TimeModel;
import com.ld.shieldsb.common.core.model.Result;

public class DateUtil extends com.ld.shieldsb.common.core.util.DateUtil {

    /**
     * 
     * 获取星期几
     * 
     * @Title getWeekOfDate
     * @author 刘金浩
     * @date 2019年9月2日 下午5:29:07
     * @param dt
     * @return String
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        return weekDays[weekOfDate(dt) - 1];
    }

    /**
     * 
     * 返回两个日期之间间隔天 时分秒
     * 
     * @Title dateDiff
     * @author 刘金浩
     * @date 2019年9月3日 上午10:56:01
     * @param startTime
     * @param endTime
     * @return TimeModel
     */
    public static TimeModel dateDiff(Date startTime, Date endTime) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;// 一秒钟的毫秒数
        // 获得两个时间的毫秒时间差异
        long diff = timeBetween(startTime, endTime);
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
        // 输出结果
        TimeModel time = new TimeModel();
        time.setDay(day > 0 ? day : 0);
        time.setHour(hour > 0 ? hour : 0);
        time.setMinite(min > 0 ? min : 0);
        time.setSeconds(sec > 0 ? sec : 0);
        return time;
    }

    /**
     * 
     * 日期和当前时间在几天内
     * 
     * @Title isDayInfo
     * @author 刘金浩
     * @date 2019年9月3日 上午9:53:22
     * @param date
     * @param interval
     * @return boolean
     */
    public static Result isDayInfo(Date date, int interval) {
        Result result = new Result();
        long beforeSeconds = interval * 24 * 60 * 60 * 1000; // 间隔天数毫秒数
        Long diffTime = timeBetween(new Date(), date); // 两个日期间隔毫秒数
        if (diffTime > 0) {
            if (diffTime > beforeSeconds) {
                result.setSuccess(false);
                result.setMessage("超过" + interval + "天");
                result.setData(0);
                return result;
            } else {
                result.setSuccess(true);
                result.setMessage(interval + "天内");
                result.setData(1);
                return result;
            }
        } else { // 过期
            result.setSuccess(false);
            result.setMessage("已过期");
            result.setData(-1);
            return result;
        }

    }

    public static void main(String[] args) {
        System.err.println(isDayInfo(str2Date("2019-09-02"), 3));
    }
}
